package site.zhangzhuo.v3;

import java.io.Serial;
import java.io.Serializable;
import java.util.HashMap;

/**
 * @ClassName Feature
 * @Author "王二麻子"
 * @Date 2024/12/11 18:40
 * 统计某字符前后字符的统计值来作为字符串的特征
 **/
public class Feature implements Serializable {
    @Serial
    private static final long serialVersionUID = 32L;

    private final Character ming;
    private final HashMap<Character,Integer> qian;
    private final HashMap<Character,Integer> hou;

    public Feature(Character ming,HashMap<Character,Integer> qian,HashMap<Character,Integer> hou){
        this.ming=ming;
        this.qian=qian;
        this.hou=hou;
    }
    public Character getMing(){
        return ming;
    }
    public HashMap<Character,Integer> getQian(){
        return qian;
    }
    public HashMap<Character,Integer> getHou(){
        return hou;
    }
    //比较两个同名特征相似度
    public float likePercent(Feature feat){
        float rst=0;
        int count=qian.size()+hou.size();
        if(!ming.equals(feat.getMing())){
            System.out.println("Feature名字不同不能比较！");
            return 0;
        }
        for(Character c: qian.keySet()){
            if(feat.getQian().containsKey(c)){
                rst+=1.0/count/(1+Math.abs(qian.get(c)-feat.getQian().get(c)));
            }
        }
        for(Character c: hou.keySet()){
            if(feat.getHou().containsKey(c)){
                rst+=1.0/count/(1+Math.abs(hou.get(c)-feat.getHou().get(c)));
            }
        }
        return rst;
    }
    @Override
    public String toString(){
        StringBuilder rst=new StringBuilder();
        rst.append(ming+"----前：");
        for(Character c: qian.keySet()){
            rst.append(c);
            rst.append(qian.get(c));
        }
        rst.append("----后：");
        for(Character c: hou.keySet()){
            rst.append(c);
            rst.append(hou.get(c));
        }
        return rst.toString();
    }
}
